package com.zhb.lkd.controller;


import com.zhb.lkd.base.PageResponse;
import com.zhb.lkd.base.ResponseResult;
import com.zhb.lkd.exception.BusinessException;
import com.zhb.lkd.pojos.dto.LoginDto;
import com.zhb.lkd.pojos.entity.User;
import com.zhb.lkd.pojos.vo.LoginVo;
import com.zhb.lkd.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author haibin
 * @since 2024-08-18
 */
@RestController
@RequestMapping("/api/user-service/user")
@Slf4j
@Api(tags = "用户相关接口")

public class UserController {

    @Resource
    private IUserService tbUserService;

    @PostMapping("/login")
    @ApiOperation("登录")
    public ResponseResult login(@RequestBody LoginDto loginDto) {
        log.info("登录{}", loginDto);
        LoginVo loginVo = tbUserService.login(loginDto);
        return ResponseResult.ok(loginVo);
    }

    @GetMapping("/search")
    @ApiOperation("用户管理")
    public ResponseResult search(
            @RequestParam(defaultValue = "1") Integer pageIndex,
            @RequestParam(defaultValue = "10") Integer pageSize, String userName) {
        log.info("用户管理{}", userName);
        PageResponse pageResponse = tbUserService.search(pageIndex, pageSize, userName);
        return ResponseResult.ok(pageResponse);
    }

    @PostMapping
    @ApiOperation("新增人员")
    public ResponseResult create(@RequestBody User user) {
        log.info("新增人员{}", user);
        tbUserService.create(user);
        return ResponseResult.ok();
    }

    @PutMapping
    @ApiOperation("修改人员")
    public ResponseResult update(@RequestBody User user) {
        log.info("修改人员{}", user);
        tbUserService.updateByUserId(user);
        return ResponseResult.ok();
    }


    @DeleteMapping("/{userId}")
    @ApiOperation("删除人员")
    public ResponseResult deleteById(@PathVariable String userId) {
        log.info("删除人员{}", userId);
        tbUserService.deleteById(userId);
        return ResponseResult.ok();
    }

}
